package org.ucn.dmaa0914.gruppe6.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.ucn.dmaa0914.gruppe6.Employee;
import org.ucn.dmaa0914.gruppe6.EmployeeCtr;

public class TopBar extends JPanel {
	private JTextField txtEmployee;
	/**
	 * Create the panel.
	 */
	public TopBar() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		JLabel lblEmployee = new JLabel("Employee:");
		add(lblEmployee);
		
		txtEmployee = new JTextField();
		txtEmployee.setBackground(UIManager.getColor("Button.background"));
		txtEmployee.setEditable(false);
		add(txtEmployee);
		txtEmployee.setColumns(10);
		
		JButton btnSelectEmployee = new JButton("Login");
		btnSelectEmployee.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectEmployee();
			}
		});
		add(btnSelectEmployee);
		
		JButton btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EmployeeCtr.getInstance().logout();
				updateValues();
			}
		});
		add(btnLogout);
		
		updateValues();
	}
	
	public void selectEmployee() {
		LoginDialog dlg = new LoginDialog();
		dlg.setVisible(true);
		
		updateValues();
	}
	
	public void updateValues() {
		Employee e = EmployeeCtr.getInstance().find(EmployeeCtr.getInstance().getCurrentEmployee());
		
		if( e != null )
			txtEmployee.setText(e.getUsername());
		else
			txtEmployee.setText("<Not logged in>");
	}
	
}
